package com.hns.can.energy.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.CarMileageService;
import com.hns.can.energy.vo.NewEnergy;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 车辆里程controller
 * @author:zhangy
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("carMileage")
public class CarMileageController extends BaseController { 

	@Autowired
	private CarMileageService carMileageService;
	
	@Autowired
    private CommonInitParamService commonInitParamService;
	
	final private String[] headerNames = new String[]{"所属机构","营运线路","车牌号","车辆编号","车辆类型", "运营日期","运行时长","运行里程"};
	final private String[] headerFields = new String[]{"organName", "lineName", "licPltNo", "carInCd", "carType", "days", "rngOprtDuraTr", "rngMile"};
	
	/**
	 * 查询车辆里程数据
	 * @param vo
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@RequestMapping("searchCarMileage")
	@ResponseBody()
	public Object searchCarMileage(NewEnergy vo) throws Exception{
		if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            return null;
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
        if(PubUtil.isNotEmpty(vo.getEnergyType())){
        	commonInitParamService.dateType2DateTime(vo, vo.getDateType());
        }
		Pagination page = carMileageService.searchCarMileage(vo);
		GridPage grid = new GridPage(page);
		return this.getReturnResult(grid);
	}
	
	/**
	 * 导出车辆里程明细excel
	 * @param response
	 * @param vo
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("exportCarMileageExcel")
	public void exportCarMileageExcel(HttpServletResponse response, NewEnergy vo) throws Exception{
		List list = getCarMileageData(vo);
		ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, 
				response, "excel", URLEncoder.encode("车辆里程明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆里程明细");
	}
	
	/**
	 * 导出车辆里程明细pdf
	 * @param response
	 * @param vo
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("exportCarMileagePdf")
	public void exportCarMileagePdf(HttpServletResponse response, NewEnergy vo) throws Exception{
		List list = getCarMileageData(vo);
		ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, 
				response, "pdf", URLEncoder.encode("车辆里程明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "车辆里程明细");
	}
	
	
	/**
	 * 获取车辆里程用于导出
	 * @param vo
	 * @return
	 * @throws Exception
	 * @author:zhangy
	 * @email:zhangyu2222z@sina.cn
	 */
	@SuppressWarnings("rawtypes")
	private List getCarMileageData(NewEnergy vo) throws Exception{
		if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            return null;
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
        if(PubUtil.isNotEmpty(vo.getEnergyType())){
        	commonInitParamService.dateType2DateTime(vo, vo.getDateType());
        }
		List list = carMileageService.getCarMileageData(vo);
		return list;
	}
}
